{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "from collections import defaultdict\n",
    "import matplotlib.pyplot as plt\n",
    "from chempy import ReactionSystem\n",
    "from chempy.kinetics.ode import get_odesys\n",
    "from chempy.kinetics.rates import SinTemp\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rsys = ReactionSystem.from_string(\"\"\"\n",
    "2 HNO2 -> H2O + NO + NO2; MassAction(EyringHS.fk('dH1', 'dS1'))\n",
    "2 NO2 -> N2O4; MassAction(EyringHS.fk('dH2', 'dS2'))\n",
    "\"\"\")  # fictitious thermodynamic parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "st = SinTemp(unique_keys='Tbase Tamp Tangvel Tphase'.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Constants:\n",
    "    Planck_constant = 6.63e-34\n",
    "    Boltzmann_constant = 1.38e-23\n",
    "    molar_gas_constant = 8.314\n",
    "\n",
    "odesys, extra = get_odesys(rsys, include_params=False, substitutions={'temperature': st}, constants=Constants())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "init_conc = defaultdict(lambda: 0, HNO2=1, H2O=55)\n",
    "params = dict(\n",
    "    Tbase=300,\n",
    "    Tamp=10,\n",
    "    Tangvel=2*math.pi/10,\n",
    "    Tphase=-math.pi/2,\n",
    "    dH1=85e3,\n",
    "    dS1=10,\n",
    "    dH2=70e3,\n",
    "    dS2=20\n",
    ")\n",
    "duration = 60"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def integrate_and_plot(system):\n",
    "    result = system.integrate(duration, init_conc, params, integrator='cvode', nsteps=2000)\n",
    "    fig, axes = plt.subplots(1, 2, figsize=(14, 4))\n",
    "    result.plot(names='NO HNO2 N2O4'.split(), ax=axes[0])\n",
    "    result.plot(names='NO2'.split(), ax=axes[1])\n",
    "    print({k: v for k, v in sorted(result.info.items()) if not k.startswith('internal')})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "integrate_and_plot(odesys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "odesys.param_names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "len(odesys.exprs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "asys = odesys.as_autonomous()\n",
    "len(asys.exprs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "[a - o for a, o in zip(asys.exprs[:-1],odesys.exprs)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "asys.exprs[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "asys.get_jac()[:-1,:-1] - odesys.get_jac()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sympy as sym\n",
    "sym.init_printing()\n",
    "args = _x, _y, _p = asys.pre_process(*asys.to_arrays(1, init_conc, params))\n",
    "args"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "asys.f_cb(*args)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "asys.j_cb(*args)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "argsode = odesys.pre_process(*odesys.to_arrays(1, init_conc, params))\n",
    "argsode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "argsode[0] - args[0], argsode[1] - args[1][:-1], argsode[2] - args[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "odesys.f_cb(*argsode)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "odesys.j_cb(*argsode)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "integrate_and_plot(asys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "odesys.ny, asys.ny"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "asys.pre_process(1, [0,1,2,3,4], [5,6,7,8])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
